import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * constantRoutes 静态路由
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }

]

/**
 * asyncRoutes 动态路由
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  // 媒体文件管理模块
  {
    path: '/media',
    component: Layout,
    hidden: true,
    redirect: '/media',
    alwaysShow: true,
    name: 'media',
    meta: {
      title: '媒体文件管理',
      icon: 'list'
    },
    children: [
      {
        path: 'video',
        component: () => import('@/views/video/index'),
        name: 'video',
        meta: { title: '视频管理', icon: 'el-icon-video-camera-solid', noCache: true }
      },
      {
        path: 'target',
        component: () => import('@/views/tag/index'),
        name: 'target',
        meta: { title: '标签管理', icon: 'tree', noCache: true }
      },
      {
        path: 'type',
        component: () => import('@/views/type/index'),
        name: 'type',
        meta: { title: '分类管理', icon: 'nested', noCache: true }
      }
    ]
  },

  // 开发工具模块
  {
    path: '/development',
    component: Layout,
    redirect: '/development',
    alwaysShow: true,
    hidden: true,
    name: 'menuInfo',
    meta: {
      title: '开发模块',
      icon: 'el-icon-s-open'
    },
    children: [
      {
        path: 'menu-info',
        component: () => import('@/views/menu-info/index'),
        name: 'MenuInfo',
        meta: { title: '菜单管理', icon: 'education', noCache: true }
      },
      {
        path: 'knife-ui',
        component: () => import('@/views/knife-ui/index'),
        name: 'knifeUi',
        meta: { title: '接口文档', icon: 'education', noCache: true }
      },
      {
        path: 'code-generator',
        component: () => import('@/views/code-generator/index'),
        name: 'codeGenerator',
        meta: { title: '代码生成器', icon: 'education', noCache: true }
      },
      {
        path: 'user-operate-info',
        component: () => import('@/views/user-operate-info/index'),
        name: 'userOperateInfo',
        meta: { title: '用户操作信息', icon: 'el-icon-document', noCache: true }
      }
    ]
  },

  {
    path: '/sys-user',
    component: Layout,
    hidden: true,
    redirect: '/sys_user',
    alwaysShow: true,
    name: 'SystemUser',
    meta: {
      title: '系统用户',
      icon: 'tree-table'
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/user/index'),
        name: 'User',
        meta: { title: '管理端用户管理', icon: 'el-icon-s-check', noCache: true }
      },
      {
        path: 'user-organization',
        component: () => import('@/views/user-organization/index'),
        name: 'UserOrganization',
        meta: { title: '组织管理', icon: 'tree', noCache: true }
      },
      {
        path: 'app-user',
        component: () => import('@/views/app-user/index'),
        name: 'User',
        meta: { title: '移动端用户管理', icon: 'peoples', noCache: true }
      }
    ]
  },

  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }

]

/**
 * 创建路由对象
 * @returns
 */
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// 导出路由
export default router
